package org.krivtsun.scanlab.examples;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.prefs.PreferencesFactory;
import java.util.prefs.Preferences;

/**
 * Created by IntelliJ IDEA.
 * User: AndreyK
 * Date: 08.10.2006
 * Time: 19:47:14
 * To change this template use File | Settings | File Templates.
 */
public class ScaleTest {

    private static final int  WIDTH = 1000;
    private static final int HEIGHT = 1000;

    public static void main(String[]  arg) {

        System.out.println("Preferences.userRoot(): "+ Preferences.userRoot().getClass());

        String pageFileName = System.getProperty("user.dir") + "\\pages\\real\\0313.tif";
        try {
            long t = System.currentTimeMillis();
            BufferedImage pageImage = ImageIO.read(new File(pageFileName));
            System.out.println("Page load time: "+(System.currentTimeMillis()-t));

            Image i = Toolkit.getDefaultToolkit().createImage(pageImage.getSource());
            System.out.println("Image class impl: "+i);

            long t1 = System.currentTimeMillis();
            pageImage.getScaledInstance(WIDTH, HEIGHT, Image.SCALE_DEFAULT);
            System.out.println("DEFAULT scale time: "+(System.currentTimeMillis()-t1));

            long t2 = System.currentTimeMillis();
            Image res = pageImage.getScaledInstance(WIDTH, HEIGHT, Image.SCALE_FAST);
            BufferedImage bi = new BufferedImage(res.getWidth(null), res.getHeight(null), BufferedImage.TYPE_INT_ARGB);
            System.out.println("FAST scale time: "+(System.currentTimeMillis()-t2));

            long t3 = System.currentTimeMillis();
            pageImage.getScaledInstance(WIDTH, HEIGHT, Image.SCALE_SMOOTH);
            System.out.println("SMOOTH scale time: "+(System.currentTimeMillis()-t3));

            double scaleX = (double)WIDTH/pageImage.getWidth();
            double scaleY = (double)HEIGHT/pageImage.getHeight();

            long t4 = System.currentTimeMillis();
            AffineTransform trans = AffineTransform.getScaleInstance(scaleX, scaleY);
            AffineTransformOp op = new AffineTransformOp(trans, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            op.filter(pageImage, null);
            System.out.println("NEAREST_NEIGHBOR AffineTransform scale time: "+(System.currentTimeMillis()-t4));

            long t5 = System.currentTimeMillis();
            trans = AffineTransform.getScaleInstance(scaleX, scaleY);
            op = new AffineTransformOp(trans, AffineTransformOp.TYPE_BICUBIC);
            op.filter(pageImage, null);
            System.out.println("BICUBIC AffineTransform scale time: "+(System.currentTimeMillis()-t5));

            long t6 = System.currentTimeMillis();
            trans = AffineTransform.getScaleInstance(scaleX, scaleY);
            op = new AffineTransformOp(trans, AffineTransformOp.TYPE_BILINEAR);
            op.filter(pageImage, null);
            System.out.println("BILINEAR AffineTransform scale time: "+(System.currentTimeMillis()-t6));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
